package org.xukai.common.spel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板值引擎，用于找出模板中键在相关领域模型中的值
 * @author JackyLIU
 *
 */
public class TemplateValueEngine {

	/**
	 * 键值解析器
	 */
	private final Map<String, IFieldResolver> fieldResolvers = new HashMap<String, IFieldResolver>();
	
	/**
	 * 注册解析器，name对应着解析器的名称，调用者可以通过配置IFieldResolver到指定的业务名称，从而方便获取解析数据
	 * @param name
	 * @param resolver
	 */
	public void register(String name, IFieldResolver resolver){
		fieldResolvers.put(name, resolver);
	}

	public Map<String, IFieldResolver> getFieldResolvers() {
		return fieldResolvers;
	}
	
	public IFieldResolver getFieldResolver(String name){
		return fieldResolvers.get(name);
	}
	
	/**
	 * 解析keys中的变量名称, 一般keys中的键值都对应着domains中的相关变量，解析之后，将返回keys中对应的值的Map
	 * @param name
	 * @param keys
	 * @param domains
	 * @return
	 */
	public Map<String, Object> resolver(String name, List<String> keys, Object... domains) throws TVEException{
		if(!fieldResolvers.containsKey(name))
			throw new TVEException("can't find name:" + name + " resolver");
	
		IFieldResolver fieldResolver = fieldResolvers.get(name);
		
		return fieldResolver.resolve(keys, domains);
	}
	
}
